<?php
namespace App\Http\Controllers\QRcode;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class QrcodeController extends Controller {
	
	public function __construct()
	{
		require_once(app_path().'/Library/QRcode/qrcode.php');
    }
	
	function ordinaryQRcode($save_path,$qr_data,$qr_level,$qr_size,$save_prefix)
	{
		(!file_exists($save_path)) && mkdir($save_path);
		
		$errorCorrectionLevel = (isset($qr_level) && in_array($qr_level, array('L','M','Q','H'))) ? &$qr_level : 'L';
		$matrixPointSize = isset($qr_size) ? min(max((int)$qr_size, 1), 10) : 4;
		
		if (isset($save_path) && isset($qr_data) && trim($qr_data) != '') {
			$filename = $save_path.$save_prefix.md5($qr_data.'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
			\QRcode::png($qr_data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);	
		}
		
		if(file_exists($save_path.basename($filename))){
			return basename($filename);
		}
		return false;
	}
	
	protected function getLogoQRcode($qr_url,$logo_url,$savepath)
	{
		$QR = imagecreatefromstring(file_get_contents($qr_url));
		$logo = imagecreatefromstring(file_get_contents($logo_url));
		$QR_width = imagesx($QR);
		$QR_height = imagesy($QR);
		$logo_width = imagesx($logo);
		$logo_height = imagesy($logo);
		$logo_qr_width = $QR_width/5;
		$scale = $logo_width/$logo_qr_width;
		$logo_qr_height = $logo_height/$scale;
		$from_width = ($QR_width - $logo_qr_width) / 2;
		imagecopyresampled ( $QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height );
		$re = imagepng ($QR,$savepath);
		return $re ? true :false;
	}
	
}